package com.gulimall.product.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.gulimall.api.feign.SearchClient;
import com.gulimall.api.feign.WareClient;
import com.gulimall.common.core.domain.Result;
import com.gulimall.common.domain.es.SkuEsModel;
import com.gulimall.product.domain.entity.*;
import com.gulimall.product.mapper.SpuInfoMapper;
import com.gulimall.product.service.*;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * spu信息 服务实现类
 * </p>
 *
 * @author 坤哥
 * @since 2025-08-25
 */
@Service
public class SpuInfoServiceImpl extends ServiceImpl<SpuInfoMapper, SpuInfo> implements ISpuInfoService {
    @Resource
    private ISkuInfoService skuInfoService;
    @Resource
    private WareClient wareClient;
    @Resource
    private IBrandService brandService;
    @Resource
    private ICategoryBrandRelationService categoryBrandRelationService;
    @Resource
    private IAttrService attrService;
    @Resource
    private IProductAttrValueService productAttrValueService;
    @Resource
    private SearchClient searchClient;

    /**
     * 商品上架
     */
    @Override
    public Result productUp(Long spuId) {
        List<SkuInfo> skuInfoList = skuInfoService.lambdaQuery().eq(SkuInfo::getSpuId, spuId).list();
        //实体类转换,准备数据
        List<SkuEsModel> skuEsModelList = skuInfoList.stream().map(skuInfo -> {
            SkuEsModel skuEsModel = new SkuEsModel();
            BeanUtil.copyProperties(skuInfo, skuEsModel);
            skuEsModel.setSkuPrice(skuInfo.getPrice());
            skuEsModel.setSkuImg(skuInfo.getSkuDefaultImg());
            skuEsModel.setHotScore(0L);
            //查询库存
            Integer stock = (Integer) wareClient.getProductStock(skuInfo.getSkuId()).getData();
            skuEsModel.setHasStock(stock > 0);
            //查询品牌信息
            Brand brand = brandService.lambdaQuery().eq(Brand::getBrandId, skuInfo.getBrandId()).one();
            skuEsModel.setBrandName(brand.getName());
            skuEsModel.setBrandImg(brand.getLogo());
            //查询分类名称
            CategoryBrandRelation categoryBrandRelation = categoryBrandRelationService.lambdaQuery().eq(CategoryBrandRelation::getCatelogId, skuInfo.getCatalogId()).list().get(0);
            skuEsModel.setCatalogName(categoryBrandRelation.getCatelogName());
            //设置规格
            List<ProductAttrValue> productAttrValues = productAttrValueService.lambdaQuery().eq(ProductAttrValue::getSpuId, skuInfo.getSpuId()).list();
            //拿到attr_id
            List<Long> attrIds = productAttrValues.stream().map(ProductAttrValue::getAttrId).collect(Collectors.toList());
            //然后去查能检索到的attr_id
            List<Attr> attrList = attrService.lambdaQuery().eq(Attr::getSearchType, 1).in(Attr::getAttrId, attrIds).list();
            List<Long> attrIdLongs = attrList.stream().map(Attr::getAttrId).collect(Collectors.toList());
            //然后去设置规格等属性
            List<ProductAttrValue> productAttrValueList = productAttrValueService.lambdaQuery().eq(ProductAttrValue::getSpuId,skuInfo.getSpuId()).in(ProductAttrValue::getAttrId, attrIdLongs).list();
            List<SkuEsModel.Attrs> attrsList = productAttrValueList.stream().map(attr -> {
                SkuEsModel.Attrs attrs = new SkuEsModel.Attrs();
                attrs.setAttrId(attr.getAttrId());
                attrs.setAttrName(attr.getAttrName());
                attrs.setAttrValue(attr.getAttrValue());
                return attrs;
            }).collect(Collectors.toList());
            skuEsModel.setAttrs(attrsList);
            return skuEsModel;
        }).collect(Collectors.toList());
        //上架数据
        return searchClient.productUp(skuEsModelList);
    }
}
